﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using com.eforceglobal.crux.BusinessFacades;
using com.eforceglobal.crux.configuration;
using com.eforceglobal.crux.configuration.FeatureManager;
using com.eforceglobal.crux.views;
using System.Security;
using com.eforceglobal.crux.security;
using com.eforceglobal.crux.DTOs;
using System.Web;
using System.Web.Security;


namespace com.eforceglobal.crux.controllers
{
    public class LoginController
    {
        ILogin _view = null;
        public LoginController(ILogin view)
        {
            _view = view;
        }

        public void InitView(bool IsPostBack)
        {
            if (!IsPostBack)
                _view.AccessDenied = false;
        }

        public void Login()
        {
            ICruxSecurityManager cruxSecurityManager = CruxSecurityManagerFactory.GetSecurityManager();
            bool AccessGranted = cruxSecurityManager.AuthenticateUser(_view.UserName, _view.Password);
            bool AccessDenied = !AccessGranted;
            _view.AccessDenied = AccessDenied;
            if (AccessGranted)
            {
                bool isResetPasswordNeeded = false;
                IFeatureManager featureManager = FeatureManagerFactory.GetFeatureManager();
                if (featureManager.IsFeatureSupported("AllowForgotPassword"))
                    isResetPasswordNeeded = cruxSecurityManager.ResetPasswordRequired(_view.UserName);
                int remainingDays = -1;
                int passwordExpiretionDays = 0;
                int passwordExpirationWarning = 0;
                if (featureManager.IsFeatureSupported("PasswordExpiry"))
                {
                    remainingDays = cruxSecurityManager.DaysRemainingForPasswordExpiration(_view.UserName);
                    passwordExpiretionDays = Convert.ToInt32(CruxConfigurationManager.GetApplicationCustomSetting("PasswordExpirationTime").Value);
                    passwordExpirationWarning = Convert.ToInt32(CruxConfigurationManager.GetApplicationCustomSetting("PasswordExpirationWarning").Value);
                }

                if (isResetPasswordNeeded)
                {
                    SetSession();
                    FormsAuthentication.SetAuthCookie(_view.UserName, false);
                    SessionData data = new SessionData();
                    data.IsFirstTimeLogin = true;
                    NavigationController.OpenView("ResetPassword", data.CurrentUser.UserID);
                }
                else if (remainingDays >= passwordExpiretionDays)
                {
                    NavigationController.OpenView("ResetPasswordAfterPasswordExpired");
                }
                else if (remainingDays >= passwordExpirationWarning)
                {
                    SetSession();
                    FormsAuthentication.SetAuthCookie(_view.UserName, false);
                    _view.IsPasswordExpirationWarningVisible = true;
                    _view.PasswordExpirationWarningDays = passwordExpiretionDays - remainingDays;
                }
                else
                {
                    SetSession();
                    FormsAuthentication.RedirectFromLoginPage(_view.UserName, false);
                }
            }
        }

        private void SetSession()
        {
            ICruxSecurityManager cruxSecurityManager = CruxSecurityManagerFactory.GetSecurityManager();
            SessionData sessionData = new SessionData();
            CompanyInformation companyInformation = cruxSecurityManager.GetCompayByUser(_view.UserName);
            UserInformation user = cruxSecurityManager.GetUserFromCompany(_view.UserName, companyInformation.CompanyName);
            sessionData.CurrentUser = user;
            sessionData.CurrentUserRole =
                string.Join(",", cruxSecurityManager.GetRolesForUser(sessionData.CurrentUser.UserID).Select(r => r.RoleName).ToArray());
            sessionData.CurrentCompany = companyInformation;
            sessionData.IsFirstTimeLogin = false;
            IFeatureManager featureManager = FeatureManagerFactory.GetFeatureManager();
            if (featureManager.IsFeatureSupported("SetLastLoginTime"))
                cruxSecurityManager.SetLastLoginTime(user.UserID);

        }

        public void NavigateToResetPassword()
        {
            SessionData data = new SessionData();
            NavigationController.OpenView("ResetPassword", data.CurrentUser.UserID);
        }

        public void SetDataInSession()
        {
            FormsAuthentication.RedirectFromLoginPage(_view.UserName, false);
        }

        public void NavigateToForgotPassword()
        {
            NavigationController.OpenView("ForgotPassword");
        }
    }
}
